package com.example.smartcitymovie.ui.selfcenter;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import androidx.room.Room;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.bumptech.glide.Glide;
import com.example.smartcitymovie.MyApplication;
import com.example.smartcitymovie.R;
import com.example.smartcitymovie.bean.UserMessageBean;
import com.example.smartcitymovie.dao.AppDatabase;
import com.example.smartcitymovie.dao.Userdao;
import com.example.smartcitymovie.databinding.FragmentSelfCenterBinding;
import com.example.smartcitymovie.entity.User;
import com.example.smartcitymovie.utils.OkHttpUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class SelfCenter extends Fragment {


    FragmentSelfCenterBinding binding;
    Userdao userdao;
    private List<String> list = Arrays.asList(new String[]{"个人信息", "订单列表", "修改密码", "意见反馈"});

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentSelfCenterBinding.inflate(inflater);

        AppDatabase db = Room.databaseBuilder(requireActivity(),
                AppDatabase.class, "database").build();
        userdao = db.userDao();
        initView();
        return binding.getRoot();
    }

    private void initView() {
        int userId = MyApplication.getSharedPreferences().getInt("userId", 0);
        if (MyApplication.getSharedPreferences().getBoolean("isLogin", false) && userId != 0) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    User user = userdao.userInfo(userId);
                    if (user != null) {
                        MyApplication.getSharedPreferencesEditor()
                                .putInt("userId", user.getId())
                                .putString("userName", user.getUserName())
                                .putString("nickName", user.getNickName())
                                .putString("email", user.getEmail())
                                .putString("phonenumber", user.getPhonenumber())
                                .putString("sex", user.getSex().toString())
                                .putString("avatar", user.getAvatar())
                                .putString("idCard", user.getIdCard())
                                .putFloat("balance", Optional.ofNullable(user.getBalance()).orElse(0f))
                                .putInt("score", Optional.ofNullable(user.getScore()).orElse(0)).apply();
                        requireActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                binding.TVUserName.setText(user.getNickName());
                                Glide.with(binding.IVUserAcavtar).load( user.getAvatar()).placeholder(R.drawable.ic_baseline_account_circle_24).into(binding.IVUserAcavtar);
                            }
                        });
                    }
                }
            });
            thread.start();

            binding.LVFunction.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list));
            binding.LVFunction.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    switch (i) {
                        case 0:
                            startActivity(new Intent(getActivity(), UserMessageActivity.class));
                            break;
                        case 1:
                            startActivity(new Intent(getActivity(), OrderListActivity.class));
                            break;
                        case 2:
                            startActivity(new Intent(getActivity(), ChangePasswordActivity.class));
                            break;
                        case 3:
                            startActivity(new Intent(getActivity(), FeedbackActivity.class));
                            break;
                    }
                }
            });
            binding.btnExit.setOnClickListener((view -> {
                if (MyApplication.getSharedPreferences().getBoolean("isLogin", false)) {
                    MyApplication.getSharedPreferencesEditor().clear().apply();
                    Navigation.findNavController(getActivity(), R.id.nav_host_fragment_activity_main).navigate(R.id.action_navigation_selfCenter_to_navigation_Login);
                }
            }));
        } else {
            Navigation.findNavController(requireActivity(), R.id.nav_host_fragment_activity_main).navigate(R.id.action_navigation_selfCenter_to_navigation_Login);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        initView();
    }
}